package com.briup.jz.service.impl;

import java.util.Date;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.jz.bean.AccountEmployee;
import com.briup.jz.bean.AccountEmployeeExample;
import com.briup.jz.bean.extend.AccountEmployeeExtend;
import com.briup.jz.dao.AccountEmployeeMapper;
import com.briup.jz.dao.extend.AccountEmployeeExtendMapper;
import com.briup.jz.service.IAccountEmployeeService;
import com.briup.jz.utils.CustomerException;

@Service
public class AccountEmployeeServiceImpl implements IAccountEmployeeService{
	@Autowired
    private AccountEmployeeMapper accountEmployeeMapper;
	@Autowired
	private AccountEmployeeExtendMapper accountEmployeeExtendMapper;
    

    //查询所有的员工
    @Override
    public List<AccountEmployee> query(Double transfer_money, String type, String status, Long userId ) {
    	AccountEmployeeExample example = new AccountEmployeeExample();
    	AccountEmployeeExample.Criteria criteria = example.createCriteria();
    	
    	if(status !=null) {
			criteria.andStatusEqualTo(status);			
		}
		if(type !=null) {
			criteria.andTypeLike("%"+type+"%");
			
		}
		if(transfer_money !=null) {
			criteria.andTransferMoneyEqualTo(transfer_money);
		}
		if(userId != null) {
    		criteria.andUserIdEqualTo(userId);
    	}
    	return accountEmployeeMapper.selectByExample(example);
    }


    //保存或更新信息
    @Override
    public void saveOrUpdate(AccountEmployee accountEmployee) throws Exception {
        if (accountEmployee.getId() == null) {
   		 // 初始化属性
        	accountEmployee.setTransferTime(new Date().getTime());
        	accountEmployee.setStatus("未审核");
            // 插入到数据库
            accountEmployeeMapper.insert(accountEmployee);
        } else {
            accountEmployeeMapper.updateByPrimaryKey(accountEmployee);
        }
    }
    //通过ID删除员工
    @Override
    public void deleteById(long id) throws CustomerException {
        AccountEmployee accountEmployee = accountEmployeeMapper.selectByPrimaryKey(id);
        if (accountEmployee == null) {
            throw new CustomerException("要删除的员工账单不存在");
        } else {
            accountEmployeeMapper.deleteByPrimaryKey(id);
        }
    }


	@Override
	public List<AccountEmployeeExtend> queryCascade(Double transfer_money, String type, String status, Long userId) {
		// TODO Auto-generated method stub
		return accountEmployeeExtendMapper.select(transfer_money, type, status, userId);
	}
}
